package main.java.indi.zyj.hot100;

import main.java.indi.zyj.hot100.base.TreeNode;

import java.util.*;

public class _40_rightSideView {
    public List<Integer> rightSideView(TreeNode root) {

        List<Integer> ans = new ArrayList<>();

        if (root == null){
            return ans;
        }

        Queue<TreeNode> q = new LinkedList<>();

        q.offer(root);

        while (!q.isEmpty()) {
            int len = q.size();
            for (int i = 0; i < len; i++) {
                TreeNode node = q.poll();
                if (node.left != null) q.offer(node.left);
                if (node.right != null) q.offer(node.right);
                if (i == len - 1){
                    ans.add(node.val);
                }
            }
        }

        return ans;
    }
}
